package com.xmy.cultivate.aspects;

import com.xmy.cultivate.annotaion.ApiMethod;
import com.xmy.cultivate.context.ApiContext;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;

@Aspect
@Component
public class ApiMethodAspect {

    @Before("@annotation(com.xmy.cultivate.annotaion.ApiMethod)")
    public void beforeMethodAccess(JoinPoint joinPoint) {
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        Method method = signature.getMethod();
        ApiMethod annotation = method.getAnnotation(ApiMethod.class);
        ApiContext.setMethodAlias(annotation.value());

        //System.out.println("访问方法: " + method.getName());
        //System.out.println("别名: " + annotation.value());
        //System.out.println("描述: " + annotation.description());
        // 可以添加日志记录、权限验证等逻辑
    }

    /*@After("@annotation(com.xmy.cultivate.annotaion.ApiMethod)")
    public void afterMethodAccess(JoinPoint joinPoint) {
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        Method method = signature.getMethod();
        ApiMethod annotation = method.getAnnotation(ApiMethod.class);

        System.out.println("访问方法: " + method.getName());
        System.out.println("别名: " + annotation.value());
        System.out.println("描述: " + annotation.description());
        // 可以添加日志记录、权限验证等逻辑
    }*/
}
